package com.example.demo.service.user;

import com.example.demo.commom.PasswordUtils;
import com.example.demo.commom.TokenUtil;
import com.example.demo.dto.user.login.ULoginDto;
import com.example.demo.dto.user.reg.PRegDto;
import com.example.demo.entity.user.Admin;
import com.example.demo.entity.user.Parent;
import com.example.demo.entity.user.Teacher;
import com.example.demo.mapper.user.ParentMapper;
import com.example.demo.vo.user.LoginVo;
import com.example.demo.vo.user.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-03-31
 * Time:18:37
 */
@Service
public class ParentService {
    @Autowired
    private ParentMapper parentMapper;

    // 注册
    public Integer reg(PRegDto pRegDto) {
        // 密码加密
        pRegDto.setPassword(PasswordUtils.encrypt(pRegDto.getPassword()));
        //注册
        return parentMapper.reg(pRegDto);
    }

    // 登录
    public LoginVo login(ULoginDto uLoginDto) {
        //校验是否可以进行登录操作
        Parent parent = parentMapper.getParentById(uLoginDto.getUser_id());
        if (parent == null || parent.getParent_id() < 5000 || parent.getParent_id() > 9999) {
            return LoginVo.returnInfo(-1, "家长编号或密码错误");
        }
        if (parent.getState() > 2 && parent.getUnfreezing_time() > System.currentTimeMillis()) {
            Long waitTime = (parent.getUnfreezing_time() - System.currentTimeMillis()) / 1000;
            return LoginVo.returnInfo(-1, "请等待"+waitTime+"s后重试");
        }
        if (parent.getState() > 2 && parent.getUnfreezing_time() <= System.currentTimeMillis()) {
            parent.setState(0);
            parentMapper.upParentState(parent);
        }
        //密码加盐处理过
        if (PasswordUtils.check(uLoginDto.getPassword(), parent.getPassword())) {
            parent.setState(0);
            parentMapper.upParentState(parent);
            return LoginVo.returnInfo(0, TokenUtil.getToken(parent.getParent_id()));
        } else {
            //1、先将state修改
            parent.setState(parent.getState() + 1);
            //2、是否修改取消冻结的最新时间
            if (parent.getState() > 2) {
                parent.setUnfreezing_time(System.currentTimeMillis() + 20000);//当前时间往后家100s
            }
            //更新state+unfreezingtime
            parentMapper.upParentState(parent);
            return LoginVo.returnInfo(-1, "家长编号或密码输入错误");
        }
    }

    // 验证家长身份
    public Boolean authentication(Integer user_id){
        Parent parent = parentMapper.getParentById(user_id);
        if (parent == null || parent.getParent_id() < 5000 || parent.getParent_id() > 9999) {
            return false;
        }
        return true;
    }

    // 查看指定家长信息
    public List<UserInfoVo> getParentById(Integer parent_id){
        Parent parent = parentMapper.getParentById(parent_id);
        if(parent == null){
            return null;
        }
        List<UserInfoVo> infos = new ArrayList<>();
        infos.add(UserInfoVo.createVoByParent(parent));
        return infos;
    }

}
